package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.EquipmentPlanDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.EquipmentPlan;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.EquipmentPlanItem;
import com.jiuqi.util.ReturnObject;

/**
 * 装备计划
 * 
 */
public class EquipmentPlanService {

	public static EquipmentPlanItem loadEquipmentPlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String eqmtId = request.getParameter("eqmtId");
			if (eqmtId == null)
				return null;
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			EquipmentPlanItem item = dao.find(Integer.parseInt(eqmtId));
			return item;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<EquipmentPlanItem> loadEquipmentPlanList(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			List<EquipmentPlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveEquipmentPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			EquipmentPlan plan = new EquipmentPlan();
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setDescription(request.getParameter("description"));
			plan.setInuseDeptId(Integer.parseInt(request.getParameter("inuseDeptId")));
			plan.setName(request.getParameter("name"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setQuantity(Integer.parseInt(request.getParameter("quantity")));
			plan.setStatus(Integer.parseInt(request.getParameter("status")));
			plan.setType(request.getParameter("type"));
			plan.setUser(request.getParameter("use"));
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyEquipmentPlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int eqmtId = Integer.parseInt(request.getParameter("eqmtId"));
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			EquipmentPlan plan = dao.find(EquipmentPlan.class, eqmtId);
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setDescription(request.getParameter("description"));
			plan.setInuseDeptId(Integer.parseInt(request.getParameter("inuseDeptId")));
			plan.setName(request.getParameter("name"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setQuantity(Integer.parseInt(request.getParameter("quantity")));
			plan.setStatus(Integer.parseInt(request.getParameter("status")));
			plan.setType(request.getParameter("type"));
			plan.setUser(request.getParameter("use"));
			dao.modify(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("修改失败！");
		}
		return ret;
	}

	public static ReturnObject removeEquipmentPlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String planId = request.getParameter("eqmtId");
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			dao.remove(EquipmentPlan.class, Integer.parseInt(planId));
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}

	public static List<EquipmentPlanItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<EquipmentPlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject removeBatchEquipmentPlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(EquipmentPlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			EquipmentPlanDAO dao = new EquipmentPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String name = request.getParameter("name");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(name)) {
			FieldBean field = new FieldBean("name", name);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
